#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020/12/4 10:47
# @Author  : Andrewq
# @Site    : nope
# @File    : departmentResource.py
# @Software: PyCharm

from flask import request, jsonify

from ..models.OrderInfoModel import OrderInfoManager
from ..models.DepartmentInfoModel import DepartmentInfoManager
from ..utils.response_code import RET


class DepartmentResource():

    @classmethod
    def get_department_person_statistics(cls):

        result_dict = DepartmentInfoManager.get_department_person_statistics()

        if result_dict.get("code") != "200":
            return jsonify(code=result_dict.get("code"), msg=result_dict.get("message"), data=[])

        return jsonify(code=RET.OK, msg=result_dict.get("message"), data=result_dict.get("data"))

    @classmethod
    def get_order_department_statistics(cls):

        result_dict = OrderInfoManager.get_order_department_statistics()

        if result_dict.get("code") != "200":
            return jsonify(code=result_dict.get("code"), msg=result_dict.get("message"), data=[])

        return jsonify(code=RET.OK, msg=result_dict.get("message"), data=result_dict.get("data"))

    @classmethod
    def get_department_all_info(cls):

        result_dict = DepartmentInfoManager.get_department_all_info()

        if result_dict.get("code") != "200":
            return jsonify(code=result_dict.get("code"), msg=result_dict.get("message"), data=[])

        return jsonify(code=RET.OK, msg=result_dict.get("message"), data=result_dict.get("data"))

    @classmethod
    def add_department_info(cls):
        department_name = request.form.get('DepartmentName')

        if not all([department_name]):
            return jsonify(code=RET.PARAMERR, msg='缺失参数user_ID', data=[])

        kwargs = {
            'DepartmentName': department_name
        }

        result_dict = DepartmentInfoManager.add_department_info(**kwargs)

        if result_dict.get("code") != "200":
            return jsonify(code=result_dict.get("code"), msg=result_dict.get("message"), data=[])

        return jsonify(code=RET.OK, msg=result_dict.get("message"), data=result_dict.get("data"))

    @classmethod
    def delete_department_info(cls):
        department_ID = request.form.get('DepartmentID')

        if not all([department_ID]):
            return jsonify(code=RET.PARAMERR, msg='缺失参数user_ID', data=[])

        kwargs = {
            'DepartmentID': department_ID,
        }

        result_dict = DepartmentInfoManager.delete_department_info(**kwargs)

        if result_dict.get("code") != "200":
            return jsonify(code=result_dict.get("code"), msg=result_dict.get("message"), data=[])

        return jsonify(code=RET.OK, msg=result_dict.get("message"), data=result_dict.get("data"))

    @classmethod
    def delete_department_info_list(cls):
        data = eval(request.form.get('data'))

        if not all([data]):
            return jsonify(code=RET.PARAMERR, msg='缺失参数data', data=[])

        kwargs = {
            'data': data
        }

        result_dict = DepartmentInfoManager.delete_employee_info_list(**kwargs)

        if result_dict.get("code") != "200":
            return jsonify(code=result_dict.get("code"), msg=result_dict.get("message"), data=[])

        return jsonify(code=RET.OK, msg=result_dict.get("message"), data=result_dict.get("data"))

    @classmethod
    def edit_department_info(cls):
        department_ID = request.form.get('DepartmentID')
        department_name = request.form.get('DepartmentName')

        if not all([department_ID, department_name]):
            return jsonify(code=RET.PARAMERR, msg='缺失参数user_ID', data=[])

        kwargs = {
            'DepartmentID': department_ID,
            'DepartmentName': department_name
        }

        result_dict = DepartmentInfoManager.edit_department_info(**kwargs)

        if result_dict.get("code") != "200":
            return jsonify(code=result_dict.get("code"), msg=result_dict.get("message"), data=[])

        return jsonify(code=RET.OK, msg=result_dict.get("message"), data=result_dict.get("data"))

    @classmethod
    def get_department_info_by_tags(cls):
        tag_name = request.args.get('tag')
        keyword = request.args.get('keyword')

        if not all([tag_name, keyword]):
            return jsonify(code=RET.PARAMERR, msg='缺失参数tag_name, keyword', data=[])

        args = {
            'tag': tag_name,
            'keyword': keyword
        }

        result_dict = DepartmentInfoManager.get_department_info_by_tags(**args)

        if result_dict.get("code") != "200":
            return jsonify(code=result_dict.get("code"), msg=result_dict.get("message"), data=[])

        return jsonify(code=RET.OK, msg=result_dict.get("message"), data=result_dict.get("data"))


